package util.ir;

import java.io.CharArrayReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.FastCharStream;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;

import util.preprocessing.StringAnalysis;

public class QueryIndex implements Constants {

	public static void multiFieldSearch(String question, String field[],
			String visualizedfield, String index,
			Hashtable<String, Float> hash, int max) throws ParseException,
			CorruptIndexException, IOException {

		String querystr = question;
		StandardAnalyzer analyzer = new StandardAnalyzer();

		MultiFieldQueryParser parser = new MultiFieldQueryParser(field,
				analyzer);

		Query q = parser.parse(question);

		IndexSearcher searcher = new IndexSearcher(index);
		// ScoreDoc[] hits = collector.topDocs().scoreDocs;
		System.out.println("Searching for: " + question);

		Hits hits = searcher.search(q);
		System.out.println("> "); // start with '> '

		int end = Math.min(hits.length(), max);

		for (int i = 0; i < end; i++) {

			Document doc = hits.doc(i);
			String name = doc.get(visualizedfield);
			if (name != null) {
				// System.out.println((i + 1) + ". " + name + " (score " +
				// hits.score(i) + ", docid 000000");
				if (hash.containsKey(name)) {

					Float f = hash.get(name);
					f = f + hits.score(i);

				} else {

					hash.put(name, hits.score(i));
				}

			} else {
				System.out
						.println((i + 1) + ". " + "No name for this document");
			}
		}

		System.out.println("\n" + hits.length() + " matching documents");

		searcher.close();

	}

	public static void standardSearch(String question, String field,
			String visualizedfield, String index,
			Hashtable<String, Float> hash, int max) throws ParseException,
			CorruptIndexException, IOException {

		String querystr = question;
		WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();

		
		if(question==null) return ;
		
		
		Query q = new QueryParser(field, analyzer).parse(querystr);
		IndexSearcher searcher = new IndexSearcher(index);
		// ScoreDoc[] hits = collector.topDocs().scoreDocs;
		System.out.println("Searching for: " + q.toString(field));

		Hits hits = searcher.search(q);
		System.out.println("> "); // start with '> '

		int end = Math.min(hits.length(), max);

		for (int i = 0; i < end; i++) {

			Document doc = hits.doc(i);
			String name = doc.get(visualizedfield);
			if (name != null) {
				System.out.println((i + 1) + ". " + name + " (score "
						+ hits.score(i) + ", docid 000000");
				if (hash.containsKey(name)) {

					Float f = hash.get(name);
					f = f + hits.score(i);

				} else {

					hash.put(name, hits.score(i));
				}

			} else {
				System.out
						.println((i + 1) + ". " + "No name for this document");
			}
		}

		System.out.println("\n" + hits.length() + " matching documents");

		searcher.close();

	}
	
	
	static IndexSearcher searcher=null;
	
	public static void indexSearch(String question, Analyzer analyzer, String field,
			String visualizedfield, String index,
			Hashtable<String, Float> hash, int max) throws ParseException,
			CorruptIndexException, IOException {

		String querystr =StringAnalysis.cleanQueryParser(question.trim());
		

	
		
		Query q = new QueryParser(field, analyzer).parse(querystr);
		if(searcher ==null)
			searcher = new IndexSearcher(index);
		// ScoreDoc[] hits = collector.topDocs().scoreDocs;
		//System.out.println("Searching for: " + q.toString(field));
		
		Hits hits = searcher.search(q);
	//	System.out.println("> "); // start with '> '

		int end = Math.min(hits.length(), max);

		for (int i = 0; i < end; i++) {

			Document doc = hits.doc(i);
			String name = doc.get(visualizedfield);
			if (name != null) {
				//System.out.println((i + 1) + ". " + name + " (score "
					//	+ hits.score(i) + ", docid 000000");
				if (hash.containsKey(name)) {

					Float f = hash.get(name);
					f = f + hits.score(i);

				} else {

					hash.put(name, hits.score(i));
				}

			} else {
			//	System.out
					//	.println((i + 1) + ". " + "No name for this document");
			}
		}

	//	System.out.println("\n" + hits.length() + " matching documents");

//		searcher.close();

	}

	public static void tokenSearch(String question, String field,
			String visualizedfield, String index,
			Hashtable<String, Float> hash, int max) throws ParseException,
			CorruptIndexException, IOException {

		String querystr = question;
		WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();

		Query q = new QueryParser(field, analyzer).parse(querystr);
		IndexSearcher searcher = new IndexSearcher(index);
		// ScoreDoc[] hits = collector.topDocs().scoreDocs;
		System.out.println("Searching for: " + q.toString(field));

		Hits hits = searcher.search(q);
		System.out.println("> "); // start with '> '

		int end = Math.min(hits.length(), max);

		for (int i = 0; i < end; i++) {

			Document doc = hits.doc(i);
			String name = doc.get(visualizedfield);
			if (name != null) {
				System.out.println((i + 1) + ". " + name + " (score "
						+ hits.score(i) + ", docid 000000");
				if (hash.containsKey(name)) {

					Float f = hash.get(name);
					f = f + hits.score(i);

				} else {

					hash.put(name, hits.score(i));
				}

			} else {
				System.out
						.println((i + 1) + ". " + "No name for this document");
			}
		}

		System.out.println("\n" + hits.length() + " matching documents");

		searcher.close();

	}

	public static void main(String argsp[]) throws CorruptIndexException,
			ParseException, IOException {

		String index = "/home/sergio/projects/indexes/index_dbpedia_entityTypes_en.txt";

		index = "/home/sergio/projects/indexes/index_dmoz.txt";

		index = "/media/sata/data/indexes/unified/";

		Hashtable<String, Float> hash = new Hashtable<String, Float>();
		standardSearch("elmo", Constants.FIELD_ANCHOR,
				Constants.WIKIPEDIA_TITLE, index, hash, 10);

		// multiFieldSearch("games", fields, "title", index, hash,20);

	}

}
